// pages/index/index.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.getUserInfo()
        wx.request({
            url: app.globalData.base_request + "/patient/login/" + app.globalData.userId,
            method: 'GET',
            success(res) {
              console.log('success');
              console.log(res.data);
              if(res.data != null)
              {
                if(res.data.role == 0)
                {
                    app.globalData.communityHospitalRole = 0;
                }
                else
                {
                    app.globalData.communityHospitalRole = 1;
                }
              }
              else
              {
                wx.request({
                    url: app.globalData.base_request + "/patient/registerUser/0/" + app.globalData.userId + "/" + app.globalData.userId + "/20/13700000000",
                    method: 'GET',
                    success(res) {
                      console.log('success');
                      console.log(res.data);
                    },
                    fail: function() {
                        console.log("failure");  
                    }
                });
                app.globalData.communityHospitalRegistered = true;
                app.globalData.communityHospitalRole = 0;
              }
              console.log(app.globalData.communityHospitalRegistered);
              console.log(app.globalData.communityHospitalRole);
            },
            fail: function() {
                console.log("failure");  
            }
        });
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    gotoDoctor:function(){
        if(app.globalData.communityHospitalRegistered && app.globalData.communityHospitalRole==1)
        {
            wx.navigateTo({
                url: '../doctorIndex/doctorIndex',
              })
        }
        else
        {
            wx.navigateTo({
                url: '../registerUser/registerUser',
              })
        }
    },

    gotoPatient:function(){
        if(app.globalData.communityHospitalRegistered && app.globalData.communityHospitalRole==0)
        {
            wx.navigateTo({
                url: '../patientIndex/patientIndex',
                })
        }
        else
        {
            wx.navigateTo({
                url: '../registerUser/registerUser',
                })
        }
    }
})